﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using SoftwareLogistics.SharepointClient.Library;

namespace SoftwareLogistics.SharepointClient.Services
{
    public class Sync
    {
        private string _siteName;
        private int _listIndex;
        private string[] _lists = { "Tasks", "Project", "Time", "Clients", "Project Portfolio" };

        public event EventHandler SyncCompleted;
        public DateTime LastSync { get; set; }

        Services.SPListService _listService;
        public Services.SPListService ListService
        {
            get
            {
                if (_listService == null)
                {
                    _listService = new SPListService();
                    _listService.GotMetaData += (sender, args) =>
                        {
                            Common.ListMetaData.Add(args.List.Name, args.List);

                            ++_listIndex;
                            if (_listIndex < _lists.Length)
                                _listService.GetListMetaData(_siteName, _lists[_listIndex]);
                            else
                            {
                                LastSync = DateTime.Now;
                                if (SyncCompleted != null)
                                    SyncCompleted(this, null);
                            }
                        };
                }

                return _listService;
            }
        }

        public void SyncNow(string siteName)
        {
            _siteName = siteName;
            Common.ListMetaData.Clear();
            ListService.GetListMetaData(siteName, _lists[0]);
        }
    }
}
